<template>
  <div class="my-pets-container">
    <el-card>
      <div slot="header">
        <span>我的宠物</span>
      </div>
      
      <el-table
        v-loading="loading"
        :data="petList"
        style="width: 100%">
        <el-table-column
          prop="name"
          label="宠物名称"
          min-width="120">
        </el-table-column>
        <el-table-column
          prop="category"
          label="类别"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="breed"
          label="品种"
          min-width="120">
        </el-table-column>
        <el-table-column
          prop="gender"
          label="性别"
          min-width="80">
        </el-table-column>
        <el-table-column
          prop="age"
          label="年龄"
          min-width="80">
          <template slot-scope="scope">
            {{ scope.row.age }}岁
          </template>
        </el-table-column>
        <el-table-column
          prop="healthStatus"
          label="健康状况"
          min-width="100">
        </el-table-column>
        <el-table-column
          label="照片"
          min-width="100">
          <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="getImageUrl(scope.row.imageUrl) || require('@/assets/default-pet.jpg')"
              fit="cover"
              :preview-src-list="[getImageUrl(scope.row.imageUrl)]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="150"
          fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleView(scope.row)">
              查看
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="handleCancelAdoption(scope.row)"
              v-if="scope.row.adoptionStatus === 'ADOPTED'">
              取消领养
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getMyPets, cancelAdoption } from '@/api/pet'

export default {
  name: 'MyPets',
  data() {
    return {
      loading: false,
      petList: []
    }
  },
  created() {
    this.fetchMyPets()
  },
  methods: {
    getImageUrl(path) {
      if (!path) return '';
      
      // 如果已经是完整的URL，直接返回
      if (path.startsWith('http://') || path.startsWith('https://')) {
        return path;
      }
      
      // 确保路径格式正确
      const cleanPath = path.startsWith('/') ? path : `/${path}`;
      
      // 返回完整的URL
      return `http://localhost:8080${cleanPath}`;
    },
    fetchMyPets() {
      this.loading = true
      getMyPets()
        .then(response => {
          if (response.code === 200) {
            console.log('My pets response:', response.data)
            this.petList = response.data
          } else {
            this.$message.error('获取我的宠物列表失败：' + response.message)
          }
        })
        .catch(error => {
          this.$message.error('获取我的宠物列表失败：' + error.message)
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleView(pet) {
      this.$router.push(`/main/pets/${pet.id}`)
    },
    handleCancelAdoption(pet) {
      this.$confirm('确认取消领养该宠物吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        cancelAdoption(pet.id)
          .then(response => {
            if (response.code === 200) {
              this.$message.success('取消领养成功')
              this.fetchMyPets()
            } else {
              this.$message.error('取消领养失败：' + response.message)
            }
          })
          .catch(error => {
            this.$message.error('取消领养失败：' + error.message)
          })
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.my-pets-container {
  padding: 20px;

  .el-card {
    margin-bottom: 20px;
  }

  .el-table {
    margin-top: 20px;
  }

  .el-image {
    border-radius: 4px;
  }
}
</style>